package com.damuzee.lvyou.user.action;

import com.damuzee.common.web.RequestUtils;
import com.damuzee.core.util.StringUtil;
import com.damuzee.core.web.WebErrors;
import com.damuzee.lvyou.user.service.UserInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by karkaw on 14-3-2.
 */
@Controller
public class UserLoginAction {

    @Resource
    private UserInfoService userInfoService ;

    @RequestMapping(value="/login",method = RequestMethod.POST)
    public String login(String userId ,String password,HttpServletRequest request){
        WebErrors errors = validateLogin(userId, password, request);
        if (!errors.hasErrors()){
            userInfoService.login(userId,password);
            return "/user/index";
        }
        return "/login";
    }

    /**
     * 检验登录表单
     *
     * @param userId
     * @param password
     * @param request
     * @return
     */
    public WebErrors validateLogin(String userId ,String password,HttpServletRequest request){
        WebErrors errors = WebErrors.create(request);

        errors.isOutOfLength(userId,"error.userId",1,10);
        errors.isOutOfLength(password,"error.password",1,10);

        return errors ;
    }
}
